/*********************************************************************************
    This software is created under the term GNU General Public License.
    You should have received a copy of the GNU General Public License.
    If not, write to the Free Software Foundation, 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

mem:
    rewrite a memory management. implement malloc, realloc and free.

author: xiangyun.kong(lo-sor@hotmail.com)
create: 2-26-2011

changes:

**********************************************************************************/
#ifndef __clib_mem_pool_h__
#define __clib_mem_pool_h__

#include <inttype.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C"{
#endif

// initialize the allocator
void pool_init();

// allocate _size bytes of memory
void* pool_alloc( uint32 _size );

// alloc a new memory with _size bytes, and then copy the old
// memory(_ptr) to the new memory. the rest of the new memory
// are uninitialized.
// if _size = 0, it do the same as pool_free
// if _ptr = 0, it do the same as pool_alloc
void* pool_realloc( void *_ptr, uint32 _size );

// free a memory which is return from pool_alloc
void pool_free( void* _ptr );

#ifdef USE_INTER_MEMORY_MANAGEMENT

#define palloc( _size )         pool_alloc( _size )
#define prealloc( _ptr, _size ) pool_realloc( (_ptr), (_size) );
#define pfree( _ptr )           pool_free( _ptr )

#else

#define palloc( _size )         malloc( _size )
#define prealloc( _ptr, _size ) realloc( (_ptr), (_size) );
#define pfree( _ptr )           free( _ptr )

#endif

#ifdef __cplusplus
};
#endif

#endif // __clib_mem_pool_h__
